import React from 'react';
class DigitalClock extends React.Component {
  constructor (props) {
    super(props)
    this.state = {
      date: new Date()
    }
  }
  componentDidMount(){
    this.timer = setInterval(()=>{
      this.setState({
        date: new Date()
      })
    },3000)
  }

  componentDidUpdate(currentProps,currentState){//更新组件也会触发render方法
    console.log(currentState);
  }
  
  componentWillUnmount(){ //在组件卸载的时候，需要把timer这个定时器删掉
    console.log('组件卸载了')
    clearInterval(this.timer)    
  }

  render () {
    console.log('运行了render方法')
    return (
      <div className="digital-clock-component jumbotron">
        <h1>{this.state.date.toLocaleTimeString()}</h1>
      </div>
    )
  }
} 
export default DigitalClock